package com.item.service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.base.CoreConstants;
import com.base.util.JPushUtil;
import com.item.dao.MessageMapper;
import com.item.dao.UserNotifyMapper;
import com.item.dao.model.Message;
import com.item.dao.model.MessageExample;
import com.item.dao.model.UserNotify;

@Service
public class MessageService {
    @Autowired
    private MessageMapper messageMapper;
    @Autowired
    private MobileVerifyService mobileVerifyService;
    @Autowired
    private UserNotifyMapper userNotifyMapper;

    private static final Logger logger = Logger.getLogger(MessageService.class);

    public int countByExample(MessageExample example) {
        return this.messageMapper.countByExample(example);
    }

    public Message selectByPrimaryKey(String id) {
        return this.messageMapper.selectByPrimaryKey(id);
    }

    public List<Message> selectByExample(MessageExample example) {
        return this.messageMapper.selectByExample(example);
    }

    public int deleteByPrimaryKey(String id) {
        return this.messageMapper.deleteByPrimaryKey(id);
    }

    public int updateByPrimaryKeySelective(Message record) {
        return this.messageMapper.updateByPrimaryKeySelective(record);
    }

    public int updateByPrimaryKey(Message record) {
        return this.messageMapper.updateByPrimaryKey(record);
    }

    public int deleteByExample(MessageExample example) {
        return this.messageMapper.deleteByExample(example);
    }

    public int updateByExampleSelective(Message record, MessageExample example) {
        return this.messageMapper.updateByExampleSelective(record, example);
    }

    public int updateByExample(Message record, MessageExample example) {
        return this.messageMapper.updateByExample(record, example);
    }

    public int insert(Message record) {
        return this.messageMapper.insert(record);
    }

    public int insertSelective(Message record) {
        return this.messageMapper.insertSelective(record);
    }
    
    /**
     * 单推
     * @param id type:1-审核相关id,2-项目id,3-会话id,4-会话id
     * @param content 推送内容
     * @param userId 指派用户id
     * @param type 0:系统消息1:审核通知2指派通知3政企互动咨询4政企互动回复5.会议通知
     */
    public void findMessage(String id,String content,String userId,Integer type){
    	UserNotify userNotify = new UserNotify();
    	userNotify.setContent(content);
    	userNotify.setIsRead(0);
    	userNotify.setRedirectType(type);
    	userNotify.setRedirectContent(id);
    	userNotify.setTitle(content);
    	userNotify.setUserId(userId);
    	userNotify.setType(1);
		int cnt = userNotifyMapper.insert(userNotify);
    	/*Message message = new Message();
    	message.setContent(content);
    	message.setNum(1);
    	message.setRedirectType(type);
    	message.setRedirectContent(id);
    	message.setTarget(userId);
    	message.setType(2);
    	message.setState(2);
    	int cnt = messageMapper.insert(message);*/
    	if (cnt > 0){
    		//推送
    		List<String> cid = mobileVerifyService.getCidsById(userId);
    		if (cid.size() > 0){
    			findPush(content, type, userId, cid);
    		}
    	}
    }
    
    /**
     * 群推
     * @param id
     * @param content
     * @param users
     * @param type
     */
    public void findMessages(String id,String content,List<String> users,Integer type){
    	UserNotify userNotify = new UserNotify();
    	userNotify.setContent(content);
    	userNotify.setIsRead(0);
    	userNotify.setRedirectType(type);
    	userNotify.setRedirectContent(id);
    	userNotify.setTitle(content);
    	userNotify.setType(1);
//    	Message message = new Message();
//		message.setRedirectType(type);
//		message.setRedirectContent(id);
//		message.setState(2);
//		message.setType(2);
//		message.setContent(content);
//		message.setNum(1);
//		message.setIsRead(0);
		for (String str : users){
//			message.setId(null);
//			message.setTarget(str);
//			messageMapper.insert(message);
			userNotify.setId(null);
			userNotify.setUserId(str);
			userNotifyMapper.insert(userNotify);
		}
		List<String> cid = mobileVerifyService.getCidByIds(users);
		if (cid.size() > 0){
			findPush(content, type, id, cid);
		}
    }
    
    /**
     * 推送
     * @param content
     * @param type
     * @param id
     * @param cid
     */
    public void findPush(String content, Integer type, String id, List<String> cid){
    	if (StringUtils.isBlank(CoreConstants.getProperty("jpush.appkey"))){
    		return;
    	}
    	Map<String, String> params = new HashMap<String, String>();
		params.put("redirectType", type+"");
		params.put("redirectContent", id);
		int num = cid.size()%1000 == 0 ? cid.size()/1000:(cid.size()/1000+1);
		for (int i = 0; i < num; i++){
			int start = i*1000;
			int end = i == (num - 1) ? cid.size():(i+1)*1000;
			List<String> pushIds = cid.subList(start, end);
			JPushUtil.pushMessageToList(params, content, pushIds);
		}
    }

    public int deleteByUser(String userId){
    	MessageExample example = new MessageExample();
    	example.createCriteria().andRedirectTypeIn(Arrays.asList(3,4)).andTypeEqualTo(2).andRedirectContentEqualTo(userId);
    	return this.messageMapper.deleteByExample(example);
    }
}